<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Cache;


class OrderController extends Controller
{
    public function index(Request $request)
    {
//        $mem_id = session('member_id');
//        订单列表
//        $data = DB::table('order')
//            ->where('disabled',1)
////            -> select('order_id','mem_id','ord_state','otime','consignee','con_region_add','con_address','con_phone','pay_mod_id','total_price')
//            ->where('mem_id','=',$mem_id)
//            ->orderBy('order_id', 'desc')
////            ->get();
        if($request->state==null){
            if ($request->keywords == '') {
                $mem_id = session('member_id');
                $data = DB::table('order')
                    ->where('disabled', 1)
                    ->where('mem_id', '=', $mem_id)
                    ->orderBy('order_id', 'desc')
                    ->get();
            } else {
                $data = DB::table('order')
                    ->where(function ($query) use ($request) {
                        $query->where('disabled', 1)
                            ->where('mem_id', '=', session('member_id'))
                            ->where('order_id', '=', $request
                                ->input('keywords'));
                    })->orderBy('order_id', 'desc')
                    ->paginate($request->input('num', 10));
            }
        }else {
            switch($request->state)
            {
                case 1:
                    $mem_id = session('member_id');
                    $data = DB::table('order')
                        ->where('disabled', 1)
                        ->where('ord_state',1)
                        ->where('mem_id', '=', $mem_id)
                        ->orderBy('order_id', 'desc')
                        ->get();
                    break;
                case 2:
                    $mem_id = session('member_id');
                    $data = DB::table('order')
                        ->where('disabled', 1)
                        ->where('ord_state',2)
                        ->where('mem_id', '=', $mem_id)
                        ->orderBy('order_id', 'desc')
                        ->get();
                    break;
                case 3:
                    $mem_id = session('member_id');
                    $data = DB::table('order')
                        ->where('disabled', 1)
                        ->where('ord_state',3)
                        ->where('mem_id', '=', $mem_id)
                        ->orderBy('order_id', 'desc')
                        ->get();
                    break;
                case 0:
                    $mem_id = session('member_id');
                    $data = DB::table('order')
                        ->where('disabled', 0)
                        ->where('mem_id', '=', $mem_id)
                        ->orderBy('order_id', 'desc')
                        ->get();
                    break;
            }
        }
        foreach($data as $v)
        {
            $arr = DB::table('order_details')
                ->select('buynum','logo_pic','title','pro_id')
                ->where('ord_id',$v -> order_id)
                ->get();
            $v -> res = $arr;
            switch($v -> pay_mod_id)
            {
                case 1:
                    $v -> pay_mod_id = '在线支付';
                    break;
                case 2:
                    $v -> pay_mod_id = '微信支付';
                    break;
                case 3:
                    $v -> pay_mod_id = '货到付款';
                    break;
                case 4:
                    $v -> pay_mod_id = '公司转账';
                    break;
            }
            $v->state_n = $v -> ord_state;
            switch($v -> ord_state)
            {
                case 1:
                    $v -> ord_state = '待付款';
                    break;
                case 2:
                    $v -> ord_state = '付款成功';
                    break;
                case 3:
                    $v -> ord_state = '等待收货';
                    break;
                case 4:
                    $v -> ord_state = '待评价';
                    break;
                case 5:
                    $v -> ord_state = '已评价';
                    break;
            }
        }
        return view('/home/myhouse/order/list',['data'=>$data]);
    }

    //订单详情
    public function show($order_id)
    {
        $mem_id = session('member_id');
        $data = DB::table('order')
            ->where('mem_id','=',$mem_id)
            ->where('order_id',$order_id)
            ->first();
            $data -> res = DB::table('order_details')
                ->where('ord_id',$data -> order_id)
                ->get();
            switch($data -> pay_mod_id)
            {
                case 1:
                    $data -> pay_mod_id = '在线支付';
                    break;
                case 2:
                    $data -> pay_mod_id = '微信支付';
                    break;
                case 3:
                    $data -> pay_mod_id = '货到付款';
                    break;
                case 4:
                    $data -> pay_mod_id = '公司转账';
                    break;

            }
        if($data->ord_state==1){
            $data->state = "待付款";
        }else{
            $data->state = "已付款";
        }
//        dd($data);
        return view('/home/myhouse/order/show',['data'=>$data]);
    }

    //立即支付
    public function pay($id)
    {
        $data = DB::table('payment_mode')->get();
       return view('/home/myhouse/order/pay',['data'=>$data,'id'=>$id]);
    }
    public function dopay(Request $request)
    {
//        //执行状态更新
        $mem_id = session('member_id');
        $order_id = $request->all()['order_id'];
        $res = DB::table('order') -> where('mem_id',$mem_id) -> where('order_id',$order_id) -> update(['ord_state'=>'2']);
//        //更新成功跳转
        if($res) {
            return redirect("/home/myhouse/order/show/$order_id")->with(['success' => '支付成功']);
        }else{
            return back() -> with(['success'=> 支付失败]);
        }
    }

    //删除订单
    public function ord_delete($order_id)
    {
        $id = session('member_id');
        $res = DB::table('order')->where('mem_id',$id)->where('order_id',$order_id)->update(['disabled'=>'0']);
        if($res){
            return redirect("/home/myhouse/order")->with(['success'=> '删除成功']);
        }else{
            return back() -> with(['success'=>'删除失败']);
        }
    }





    public function create(Request $request)
    {
    	//var_dump($request->all());
    	$mem_id=session('member_id');
        //var_dump($mem_id);
    	$rec_adds=DB::table('rec_add')->leftJoin('region','rec_add.con_region','=','region.region_id')->where('mem_id','=',$mem_id)->get();
    	//var_dump($rec_adds);
    	 //支付方式缓存
        if(!Cache::has('payment_modes'))
        {
            $payment_modes=DB::table('payment_mode')->get();
            Cache::forever('payment_modes',$payment_modes);
        }
        $payment_modes=Cache::get('payment_modes');
        //var_dump($payment_modes);

        //购物
        $orderpros=DB::table('cart')->leftJoin('product','product.product_id','=','cart.pro_id')->where('mem_id','=',$mem_id)->whereIn('cart_id',$request->cart_id)->get();
        //var_dump($orderpros);
    	return view('home/main/ordercreate',['rec_adds'=>$rec_adds,'payment_modes'=>$payment_modes,'orderpros'=>$orderpros]);
    }
    //处理添加订单
    public function store(Request $request)
    {
    	//var_dump($request->all());
    	$mem_id=session('member_id');
    	$reg_add=DB::table('rec_add')->leftJoin('region','region.region_id','=','rec_add.con_region')->where('mem_id','=',$mem_id)->where('rec_add.rec_add_id','=',$request->rec_add)->get();
		//var_dump($reg_add);
		$consignee=$reg_add[0]->consignee;
		$con_region_add=$reg_add[0]->region_name;
		$con_address=$reg_add[0]->con_address;
		$con_phone=$reg_add[0]->con_phone;
		$pay_mod_id=$request->pay_mod;
		$otime=time();

		$ptime=time();

		$ord_state=1;
		//var_dump($consignee,$con_region_add,$con_address,$con_phone,$pay_mod_id,$otime,$ptime,$ord_state);
		$cartpros=DB::table('cart')->leftJoin('product','product.product_id','=','cart.pro_id')->whereIn('cart_id',$request->cart_id)->get();
		//var_dump($cartpros);
		$allbuynum=0;
		$everyuprice=0;
		$alluprice=0;
		foreach($cartpros as $cartpro)
		{
			$allbuynum+=$cartpro->buynum;
			$everyuprice=($cartpro->sprice+$cartpro->lprice)*$cartpro->buynum;
			$alluprice+=$everyuprice;
		}
		//var_dump($alluprice);
		//var_dump($allbuynum);
		$mol=ceil($allbuynum/5);
		//var_dump($mol);
		$freight=$mol*$reg_add[0]->freight;
		//var_dump($freight);

		$total_price=$alluprice+$freight;
		//var_dump($total_price);
		//添加订单
		$orderid=DB::table('order')->insertGetId(['mem_id'=>$mem_id,'consignee'=>$consignee,'con_region_add'=>$con_region_add,'con_address'=>$con_address,'con_phone'=>$con_phone,'pay_mod_id'=>$pay_mod_id,'freight'=>$freight,'total_price'=>$total_price,'message'=>$request->ordermessage,'ord_state'=>$ord_state,'otime'=>$otime]);
		//var_dump($orderid);


		
		foreach($cartpros as $cartpro)
		{
			$uprice=$cartpro->sprice+$cartpro->lprice;
			DB::table('order_details')->insert(['ord_id'=>$orderid,'pro_id'=>$cartpro->pro_id,'cosir_id'=>$cartpro->cosir_id,'title'=>$cartpro->title,'uprice'=>$uprice,'logo_pic'=>$cartpro->logo_pic,'pro_keyword'=>$cartpro->pro_keyword,'pro_code'=>$cartpro->pro_code,'buynum'=>$cartpro->buynum]);
			//更改购买量
			DB::table('product')->where('product_id','=',$cartpro->pro_id)->increment('sellnum',$cartpro->buynum);
            //更改库存
            DB::table('pro_attr')->where('pro_id','=',$cartpro->pro_id)->where('cosir_id','=',$cartpro->cosir_id)->decrement('attr_stocknum',$cartpro->buynum);
            DB::table('product')->where('product_id','=',$cartpro->pro_id)->decrement('stocknum',$cartpro->buynum);
		}
			
		//删除购物
		DB::table('cart')->whereIn('cart_id',$request->cart_id)->delete();

		

		return redirect()->route('myorder');



    }

}
